/****************************************
* James Gomez                           *
* Project 3: Target Practice P3         *
* Comp 465                              *
* Professor Barnes                      *
* 12/7/2012                             *
*****************************************/

#ifndef _MOON_
#define _MOON_

#include "GomezJamesP3_OpenGL.h"
#include "GomezJamesP3_CollidableObject.cpp"
#include "GomezJamesP3_Movable.cpp"

#include "GomezJamesP3_MissileBattery.cpp"

#include <string>
using namespace std;

/*************************************************
Collidable Moon class that orbits around planets. 
Moons may contain 0 to 1 missile batteries.
**************************************************/
class Moon: public CollidableObject, Movable{

private:
	Object3D * anchor;
	GLdouble radius;
	GLfloat angle, angleIncrement;
	Color color[3];
	GLfloat offset[3];		

public:
	Moon(const string name, const GLdouble r, const Color c[3], Object3D * anchor,
		const GLfloat offset[3], const GLfloat angleInc);
	~Moon(){}

	void draw();
	void move();

};


Moon::Moon(const string name, const GLdouble r, const Color c[3], Object3D * anchor,
		   const GLfloat offset[3], const GLfloat angleInc): CollidableObject(name, r){

	this->anchor = anchor;
	this->radius = r;
	this->angleIncrement = angleInc;
	this->angle = 0;
	
	for (int i=0; i<3; i++){
		this->color[i] = c[i];
		this->offset[i] = offset[i];	
	}
}

void Moon::draw(){
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glMultMatrixf(TM);
	glColor3fv(color);
	glutSolidSphere(radius, SPHERE_SLICES, SPHERE_STACKS);
	glPopMatrix();	
}

void Moon::move(){

	angle += angleIncrement;
	if (angle >= 360) angle = 0;	

	glMatrixMode(GL_MODELVIEW);	
	glPushMatrix();
	resetTM();
	GLfloat m[16];
	anchor->getTM(m);
	glLoadMatrixf(m);
	glRotatef(angle, 0, 1, 0);
	glTranslatef(offset[0], offset[1], offset[2]);
	glGetFloatv(GL_MODELVIEW_MATRIX, TM);
	glPopMatrix();
};


#endif